package com.example.project22_editingmediaplayer;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.minimusicdemo.Main_play;
import com.example.project22_editingmediaplayer.action.AudioUltility;
import com.example.project22_editingmediaplayer.adapter.RowSong;
import com.example.project22_editingmediaplayer.objects.SongObject;
import com.example.project22_editingmediaplayer.resource.SongResource;
import com.example.provider.SamsungProvider;
import com.example.provider.TblSong;

public class PlaylistAlbumDetail extends Activity {
	Context myContext;

	ListView listView_detail;
	ArrayList<SongObject> listSongAlbum;
	RowSong rowAdapterSongAlbum;
	int positionOfItem;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listplay_detail);
		positionOfItem = getIntent().getIntExtra("position", -1);
		myContext = getApplicationContext();
		listView_detail = (ListView) findViewById(R.id.lv_playlistdetail);
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);

		rowAdapterSongAlbum = new RowSong(listSongAlbum, myContext);
		listView_detail.setAdapter(rowAdapterSongAlbum);
		new LoadAllSongInAlbum().execute();

		listView_detail
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						SongResource.listSong.clear();
						SongResource.listSong.add((SongObject) listView_detail
								.getItemAtPosition(arg2));
						MainActivity.mService.PlaySong(0);
						Intent intent = new Intent(PlaylistAlbumDetail.this,
								Main_play.class);
						MainActivity.mService.flag_first = false;
						startActivity(intent);

					}
				});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub

		getMenuInflater().inflate(R.menu.menuplayemotion, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		return super.onMenuItemSelected(featureId, item);
	}

	// xu ly back lai man hinh truoc
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		case R.id.playall:
			// finish();
			try {
				SongResource.listSong.clear();
				SongResource.listSong = rowAdapterSongAlbum.getAllData();
				Intent intent = new Intent(PlaylistAlbumDetail.this,
						Main_play.class);
				MainActivity.mService.PlaySong(0);
				startActivity(intent);
			} catch (Exception e) {
				// TODO: handle exception
				Toast toast = Toast.makeText(myContext, "no song in list",
						Toast.LENGTH_LONG);
				toast.show();
			}

			break;

		default:
			break;
		}

		return true;
	}

	private class LoadAllSongInAlbum extends
			AsyncTask<Void, Void, ArrayList<SongObject>> {
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub

			super.onPreExecute();
		}

		@Override
		protected ArrayList<SongObject> doInBackground(Void... params) {
			// TODO Auto-generated method stub
			ArrayList<SongObject> arrayList = new ArrayList<SongObject>();
			AudioUltility audioUltility = new AudioUltility(myContext);
			Cursor c = null;
			switch (positionOfItem) {
			case 0:
				c = myContext.getContentResolver().query(
						SamsungProvider.URI_SONG, null,
						TblSong.SONG_ISFAVORITE + " =?",
						new String[] { "yes" }, null);
				break;
			case 1:
				c = myContext.getContentResolver().query(
						SamsungProvider.URI_SONG, null,
						TblSong.SONG_GENRE + " =?", new String[] { "happy" },
						null);
				break;
			case 2:
				c = myContext.getContentResolver().query(
						SamsungProvider.URI_SONG, null,
						TblSong.SONG_GENRE + " =?", new String[] { "funny" },
						null);
				break;
			case 3:
				c = myContext.getContentResolver().query(
						SamsungProvider.URI_SONG, null,
						TblSong.SONG_GENRE + " =?", new String[] { "sad" },
						null);
				break;
			}
			if (c != null) {
				while (c.moveToNext()) {
					arrayList
							.add(new SongObject(
									c.getInt(c
											.getColumnIndexOrThrow(TblSong.SONG_ID)),
									audioUltility
											.getOneSongById(
													c.getInt(c
															.getColumnIndexOrThrow(TblSong.SONG_ID)))
											.get_path(),
									audioUltility
											.getOneSongById(
													c.getInt(c
															.getColumnIndexOrThrow(TblSong.SONG_ID)))
											.get_name(),
									audioUltility
											.getOneSongById(
													c.getInt(c
															.getColumnIndexOrThrow(TblSong.SONG_ID)))
											.get_artist(),
									c.getString(c
											.getColumnIndexOrThrow(TblSong.SONG_GENRE)),
									c.getString(c
											.getColumnIndexOrThrow(TblSong.SONG_ISFAVORITE))));

				}
				c.close();
				return arrayList;
			} else {
				c.close();
				return null;
			}
		}

		@Override
		protected void onPostExecute(ArrayList<SongObject> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			rowAdapterSongAlbum.resetData(result);
		}
	}
}